package com.peg;

import com.peg.dataobjects.Context;
import com.peg.exceptions.ParserException;
import com.peg.rules.*;
import java.util.Map;

/**
 * @author sad
 */
public class BaseParser {

    private Map<String, Rule> rules;
    private Context lastContext;

    public BaseParser(Map<String, Rule> rules) {
        this.rules = rules;
    }

    public boolean parse(String textToParse) {
        Rule rule = rules.get("StartRule");
        if (rule == null) {
            throw new ParserException("StartRule is not provided in grammar", -1);
        }

        lastContext = new Context();
        lastContext.setRulesMap(rules);
        lastContext.setStream(textToParse, true);
        return rule.execute(lastContext);
    }

    public Context getLastContext() {
        return lastContext;
    }
}
